package com.homegoods.resource;

import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.text.TextUtils;

import com.danikula.videocache.HttpProxyCacheServer;
import com.homegoods.integration.AppDelegate;
import com.homegoods.integration.AppLifecycles;
import com.kedll.library.KBaseApplication;
import com.kedll.library.util.SharePreUtils;
import com.kedll.library.util.ToastUtil;

/**
 * @author QiQi
 * @ClassName: HomeGoodsApp
 * @date 2018/9/2
 * @description
 */
public class HomeGoodsApp extends KBaseApplication {

    private AppLifecycles mAppDelegate;
    private HttpProxyCacheServer proxy;
    private static String token;
    private static String OSSHost;

    public static HttpProxyCacheServer getProxy(Context context) {
        HomeGoodsApp app = (HomeGoodsApp) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer.Builder(this)
                .maxCacheFilesCount(20)
                .build();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (mAppDelegate != null) {
            mAppDelegate.onCreate(this);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
    }

    @Override
    protected String getHost() {
        return null;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        if (mAppDelegate != null) {
            mAppDelegate.onTerminate(this);
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        if (mAppDelegate == null) {
            mAppDelegate = new AppDelegate(base);
        }
        this.mAppDelegate.attachBaseContext(base);
    }

    public static String getOSSHost() {
        if (OSSHost == null) {
            if (SharePreUtils.getObject("OSSHost") != null) {
                return SharePreUtils.getObject("OSSHost").toString();
            } else {
                ToastUtil.showToast("获取OSS地址失败");
            }
        }
        return OSSHost;
        //临时写死，等后台
//        return "http://ihome-zpzy.oss-cn-hangzhou.aliyuncs.com/";
    }

    public static String getToken() {
        if (token == null) {
            if (SharePreUtils.getObject("token") != null) {
                return SharePreUtils.getObject("token").toString();
            } else {
                ToastUtil.showToast("请先登录");
            }
        }
        return token;
    }

    public static void setOSSHost(String OOSHost) {
        SharePreUtils.saveObject("OSSHost", OOSHost);
    }

    public static void setToken(String token) {
        SharePreUtils.saveObject("token", token);
        HomeGoodsApp.token = token;
    }

    public static void deleteToken() {
        token = null;
        SharePreUtils.saveObject("token", "");
    }


    public static boolean isLogin() {
        String userData = SharePreUtils.getAccount(instance);
        if (TextUtils.isEmpty(userData)) {
            return false;
        }
        return true;
    }
}
